<?php
/**
 * 账号
 * 
 * @author kaikaic
 * @date 2018-04-09
 *
 */

namespace app\api\service;

use app\admin\service\AdvService;
use service\ConfigService;
use think\Db;

class   MainService{
    /**
     * 根据广告位ID加载广告内容
     * @param $spotID,广告位ID
     * @return array
     */
    public static  function loadAD($spotID) {

        $adv =  Db::name('SystemAdv')->where('spot_id', $spotID)->select();

        $adList = array();
        for ($i=0; $i<count($adv); $i++) {
            $url = $adv[$i]['act_type'].$adv[$i]['act_id'];
            $adList[] = array('image'=>$adv[$i]['image'], 'link'=>$adv[$i]['link'],'name'=>$adv[$i]['name'],'url'=>$url);
        }

        return array('type'=>'single', 'ad'=>$adList);
    }
    //获取订单列表
    public static function getOrderList($accountid,$order_status,$page,$size){
        if($order_status){
            if($order_status==1){
                $orderList  = Db::name('exam_order')
                    ->where('account_id', $accountid)
                    ->where('order_status',1)
                    ->where('pay_status',1)
                    ->order('create_time desc')
                    ->limit($page*$size,$size)->select();
            }else if($order_status==2){
                $orderList  = Db::name('exam_order')
                    ->where('account_id', $accountid)
                    ->where('pay_status',2)
                    ->order('create_time desc')
                    ->limit($page*$size,$size)->select();
            }else{
                $orderList  = Db::name('exam_order')
                    ->where('account_id', $accountid)
                    ->where('order_status',$order_status)
                    ->order('create_time desc')
                    ->limit($page*$size,$size)->select();
            }

        }else{
            $orderList  = Db::name('exam_order')->where('account_id', $accountid)->order('create_time desc')->limit($page*$size,$size)->select();

        }

        foreach ($orderList as $key=>&$item){
            $item['mec'] = Db::name('exam_mec')->find($item['mec_id']);
            $set_idres = explode(',',$item['set_id']);

            $setname = '';
            for($i=0;$i<count($set_idres);$i++){
                $item['set'][$i] = Db::name('exam_set')->find($set_idres[$i]);
                $setname = $setname.'.'.$item['set'][$i]['name'];
            }
            if($item['order_status']==4){
                $item['report'] = Db::name('exam_report')->where('order_sn',$item['order_sn'])->find();
            }
            $item['setname'] = $setname;
            $item['create_time'] = date('Y-m-d H:i:s',$item['create_time']);

        }
        return $orderList;
    }

    //获取文章
    public static function getArticle($art_id){
        $artInfo = Db::name("articles")->find($art_id);
        return $artInfo;
    }
    public static function getArticleType($arttype){
        $artList = Db::name("articles")->where('category_id',$arttype)->select();
        return $artList;
    }

    public static function getOrderInfoByPaySn($paysn){
        $res = Db::name("exam_order")->where('pay_sn',$paysn)->find();

        return $res;
    }
    public static function getOrderInfoByOrdersn($ordersn){
        $res = Db::name("exam_order")->where('order_sn',$ordersn)->find();
        $res['mec'] = Db::name('exam_mec')->find($res['mec_id']);
        $set_idres = explode(',',$res['set_id']);

        $setname = '';
        for($i=0;$i<count($set_idres);$i++){
            $res['set'][$i] = Db::name('exam_set')->find($set_idres[$i]);
            $setname = $setname.$res['set'][$i]['name'].'.';
        }
        if($res['order_status']==4){
            $res['report'] = Db::name('exam_report')->where('order_sn',$res['order_sn'])->find();
        }
        $res['setname'] = $setname;
        $statusArr = ConfigService::cfgOrderStatus();
        $res['order_status_txt'] = $statusArr[ $res['order_status']];
        $res['create_time'] = date('Y-m-d H:i:s',$res['create_time']);
        $res['update_time'] = date('Y-m-d H:i:s',$res['update_time']);
        $res['cancel_time'] = date('Y-m-d H:i:s',$res['cancel_time']);
        return $res;
    }
    public static function cancelOrder($ordersn,$data){
        $res = Db::name("exam_order")->where('order_sn',$ordersn)->update($data);
        return $res;
    }
    public static function getMemberList($accountid){
        $memberList  = Db::name('account_member')->where('account_id', $accountid)->select();
        return $memberList;
    }
    public static function findMember($id){
        $memberInfo  = Db::name('account_member')->find($id);
        return $memberInfo;
    }
    public static function saveMemberInfo($data){
        if(isset($data['id'])){
            $id = $data['id'];
            unset($data['id']);
            $memberInfo  = Db::name('account_member')->where('id',$id)->update($data);
            if($memberInfo>=0){
                return true;
            }else{
                return false;
            }
        }else{
           if(Db::name('account_member')->insert($data)){
               return true;
           }else{
               return false;
           }
        }
    }
}
